﻿var shopID = "";
var CityID = "";
var ulMessage = "";
var categoryID = "";
var userName = "";
function initCommentary() {
    //得到当前用户名
    if (document.getElementById("userName")) {
        userName = document.getElementById("userName").innerHTML;
    } else {
        userName = "游客";
    }
    
//通过页面上的URL来得到CITYID,CATEGORYID,TRADEMARKID
    var locationUrl = window.location.href;
    var arrUrl = locationUrl.split("/");
    var TrademarkID = arrUrl[arrUrl.length - 1];
    var arrshopID = TrademarkID.split('?');
    //得到SHOPID,即TRADEMARKID
    shopID = arrshopID[0];
    var ID = arrshopID[1].split('&');
    var arrCityID = ID[0].split('=');
    //得到CITYID
    CityID = arrCityID[1];
    var category = ID[1].split('=');
    //得到CATEGORYID
    categoryID = category[1];
    //对所有方法进行初始化
    getTrademark(shopID);
    readCommentary(shopID);
    getCityName(CityID);
    getCityWeather(CityID);
    getCityImg(CityID);
    getCityMessage(CityID);
    var TrademarkID1 = TrademarkID.split('#');
    //给页眉上的几个连接增加连接地址
    document.getElementById("backCity").href = '../../City/CityDetail/' + CityID;
    //给第一个TD添加文字
    $("#backCountry").html('日本');
    document.getElementById("backCountry").href = '../../Home/Index';
    document.getElementById("backCategory").href = '../../City/CityDetail/' + CityID;
    document.getElementById("backShop").href = '../../City/Trademark/' + TrademarkID1[0];
    document.getElementById("commeryImgUrl").href = '../../City/MoreCityImg/' + TrademarkID1[0];
    
}

//得到现在所在商户的信息
function getTrademark(shopID) {
//通过JSON来查询相应的数据
    $.getJSON('/City/getTrademark/' + shopID + "," + CityID, { t: new Date() }, function(j) {

        var arr = new Array();
        //当查询出来的为有数据的信息时进行数据赋值
        if (j[0] != '没有相关的信息!') {
            for (var i = 0; i < j.length; i++) {

                var values = j[i];
                // alert(values);
                arr = values.split('★');
                //alert(arr.length);
                //得到商户的LOGO名
                var shopLogo = arr[0];
                //得到商户的中文名
                var chineseName = arr[1];
                //得到商户的日文名
                var japonicName = arr[6];
                //判断商户是否有日文名
                if (japonicName == "" || japonicName == null) {
                    japonicName = '无';
                }
                //得到商户的英文名
                var englishName = arr[8];
                //得到商户的官方网站
                var officialweb = arr[4];
                //得到商户的地址
                var shopadd = arr[2];
                //得到商户的交通信息
                var shoptraffic = arr[5];
                //得到商户的营业时间
                var businesshours = arr[9];

                //得到商户的联系方式
                var tel = arr[7];
                //得到商户的详细信息
                var message = arr[3];
                //把得到的所有信息添加到相应的标签中
                document.getElementById("shopLogo").src = '/Content/img/shop/81/' + shopID + '/' + shopLogo;
                $("#chineseName").html(chineseName);
                $("#lb_shopName").html(chineseName);
                $("#englishName").html(englishName);
                $("#japonicName").html(japonicName);
                $("#businessHours").html(businesshours);
                $("#shopAdd").html(shopadd);
                $("#shoptraffic").html(shoptraffic);
                $("#tel").html(tel);
                $("#officialweb").html(officialweb);
                $("#shopMessage").html(message);

            }
        }

    })
}

//当用户点击提交以后进行相应值的提取
function getCommentaryMessage() {
    //产品
    var product = document.getElementById("review_s1").options[document.all("review_s1").selectedIndex].text;
    //环境
    var condition = document.getElementById("review_s2").options[document.all("review_s2").selectedIndex].text;
    //服务
    var server = document.getElementById("review_s3").options[document.all("review_s3").selectedIndex].text;
    //价位
    var price = document.getElementById("review_ap").options[document.all["review_ap"].selectedIndex].text;
    //评价
    var appraise = document.getElementById("review_body").value;
    //喜爱程度
    var obj = document.getElementsByName("review_form_f");
    //标题
    var shopName = document.getElementById("shopName").innerHTML;
    
    var loveLe;
    for (var i = 0; i < obj.length; i++) {
        if (obj[i].checked) {
            var s = obj[i].value;
            loveLe = document.getElementById("reviewlb" + s).innerText;
        }
    }
    //分类标签
    var tab = document.getElementById("review_form_stag").value;
    //交通停车
    var traffic = document.getElementById("review_form_park").value;

    //判断必填项是否已经填写
    if (document.all("review_s1").selectedIndex == 0) {
        alert("请选择产品评价!");
        return false;
    } else if (document.all("review_s2").selectedIndex == 0) {
        alert("请选择环境评价!");
        return false;
    } else if (document.all("review_s3").selectedIndex == 0) {
        alert("请选择服务评价!");
        return false;
    } else if (document.all["review_ap"].selectedIndex == 0) {
        alert("请选择价位评价!");
        return false;
    } else if (appraise == "" || appraise == null) {
        alert("请填写评价!");
        return false;
    } else {
    
    //把所有需要记录到数据库的字段通过JSON写到后台中
    $.getJSON('/City/CommentaryAction/' + product + "," + condition + "," + server + "," + price + "," + appraise + "," + loveLe + "," + tab + "," + traffic + "," + shopName + "," + shopID+","+userName, { t: new Date() }, function(j) {
        var jsonvalue = j[0];

        if (jsonvalue == "OK") {
            //如果添加成功就刷新该页面
            window.location.href(shopID+"?CityID="+CityID+"&CategoryID="+categoryID);
        } else {
            alert("评论插入失败!");
        }
    })
    }

}

//读取相应评论功能
function readCommentary(shopID) {
    var s = "";
   
    var tbody = document.getElementById("testTable");
    //通过JSON来得到相应商户的评论信息
    $.getJSON('/City/readCommentary/' + shopID, { t: new Date() }, function(j) {
        if (j.length != 0) {
            for (var i = 0; i < j.length; i++) {
                //得到相应的评论数据
                var value = j[i];
                //分割数据
                var arr = value.split('|');
                if (arr[0] != "尚没有评论") {
                    //对TABLE的初始化
                    var tr1 = document.getElementById("testTable").insertRow();
                    var td1 = tr1.insertCell();

                    td1.align = 'center';

                    td1.innerHTML = "<table width='100%'><tr><td  rowspan='2' align='center' style='border:2px solid #FFE9CA' width='18%' valign='top'><table cellpadding='0' cellspacing='0' width='30%'><tr><td valign='top'><img src='../../Content/img/commentary/image1.gif' /></td></tr><tr><td valign='top'>网友用户名：" + arr[0] + "</td></tr></table></td><td width='100%' valign='top'><table cellpadding='0' cellspacing='0' width='100%'><tr><td valign='top' style='border:2px solid #FFE9CA'>此评论发表于：" + arr[1] + "</td></tr><tr><td class='style4'  style='border:2px solid #FFE9CA' align='left' valign='top'>" + arr[2] + "</td></tr></table></td></tr></table>";
                } else {
                    document.getElementById("testTable").style["visibility"] = "hidden";
                    $("#noCommentary").html('暂无任何评论内容!');
                    break;
                }

            }
        }
        else {
            document.getElementById("testTable").style["visibility"] = "hidden";
            $("#noCommentary").html('暂无任何评论内容!');
        }
    })
}


//得到该城市的天气信息
function getCityWeather(CityID) {

    var param = "/Util/getCityWeather/" + CityID;
    //通过JSON得到该城市的天气信息
    $.getJSON(param, { t: new Date() }, function(j) {

        var arr = new Array();
       
        for (var i = 0; i < j.length; i++) {
            var value = j[i];
            //把得到的相应的信息写到对应的标签中
            document.getElementById("WeatherCity" + (i + 1)).innerHTML = value;
        }

    });
}


function getCityImg(CityID) {

//通过JSON得到城市的市标
    $.getJSON('/Util/getCityImg/' + CityID, { t: new Date() }, function(j) {
        var value = j[0];
        if (value == 'no image') {
            document.getElementById("testCityImg").innerHTML = "";
            
        } else {
          
            document.getElementById("testCityImg").innerHTML = "<img width='60px' height='60px' id='img_CityImg' src = '" + value + "'/>";

        }
    })
}

//得到城市中文名字
function getCityName(CityID) {
    var url = "/City/GetCityMessage/" + CityID;
//通过调用JSON里的ACTION来得到城市的中文名
    $.getJSON(url, { t: new Date() }, function(j) {
        var arr = new Array();

        for (var i = 0; i < j.length; i++) {

            var vaule = j[i];
           
            arr = vaule.split('|');
            
            var cityNameCH = arr[0];
            var cityNameEN = arr[2];
            var cityNameJP = arr[3];
            //在得到的相应的数据里填充到相应的标签中
            document.getElementById("CityName").innerHTML = cityNameCH;
            
        }
    })
}



//得到对应的城市信息
function getCityMessage(CityID) {
    
    var url = "/City/GetCityMessage/" + CityID;
    //通过JSON来得到城市信息
    $.getJSON(url, { t: new Date() }, function(j) {

        var arr = new Array();

        for (var i = 0; i < j.length; i++) {
            var vaule = j[i];
            
            arr = vaule.split('|');
            //得到城市中文名
            var cityNameCH = arr[0];
            //得到城市的英文名
            var cityNameEN = arr[2];
            //得到城市的日文名
            var cityNameJP = arr[3];
            //把得到值填充到响应的标签中
            document.getElementById("cityNameCH").innerHTML = cityNameCH;
            document.getElementById("cityNameEN").innerHTML = cityNameEN;
            document.getElementById("cityNameJP").innerHTML = cityNameJP;
          
            document.getElementById("CityName").innerHTML = cityNameCH;
            //得到城市完全信息内容
            var cityMessage = arr[4];
            //初始化信息的字数
            var num = 55;
            //记录最后结束字数
            var sum = 1;
            
            for (var k = 0; k < cityMessage.length; k++) {
                //对信息进行SUBSTRING,得到相应字数数据
                var arrCityMessage = cityMessage.substring(k, num);
                
                ulMessage += arrCityMessage + "|";
                //得到每次SUBSTRING的开始字数
                k += 54;
                //得到每次SUBSTRING的结束字数
                if (cityMessage.length - num < 55) {

                    num = cityMessage.length;

                } else {
                    num = k + 56;
                }
                sum += 1;

            }
            
            var arrulMessage = ulMessage.split('|');
            
            //动态生成相应的LI标签            
            var ul = document.getElementById("testLiMessage");

            for (var b = 0; b < arrulMessage.length - 1; b++) {
                
                var li = document.createElement("li");
                var t = document.createTextNode(arrulMessage[b]);
                li.appendChild(t);
                ul.appendChild(li);
            }
            //将填充好数据的子标签添加到相应的父标签中
            $('#testLiMessage').innerfade({
                animationtype: 'fade',
                speed: 750,
                timeout: 4000,
                type: 'sequence',
                containerheight: '1em'
            });
        }


    });
    //得到城市信息以后把生成标签



}

//点击官方网站后的条转


function openTrademarkWeb() {
    //alert();
    //$("#officialweb").attr("href", document.getElementById("officialweb").innerHTML);
    var s = document.getElementById("officialweb").innerHTML;
    var st = s.split(':');
    if (st[0] != 'http') {
        s = 'http://' + document.getElementById("officialweb").innerHTML;
    }
    window.open(s, 'test', 'height=400, width=800, top=0, left=0, toolbar=yes, menubar=yes, scrollbars=yes, resizable=yes,location=yes, status=yes');
}